﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WeatherView
{
   //
    // This is a Universal app that demonstrates using two web services to get data to update a single
    // shared XAML-based user interface.
    // 
    // The app uses the location of the device to query the Weather Underground website, and obtain
    // the current weather conditions, and some forecasts.
    //
    // The weather conditions are sent to Bing to obtain a related image, which is then displayed.
    //
    // HttpClient and Windows.Data.Json are used to obtain and parse the internet data. 
    //
    

    //
    // This particular class contains only the code to reposition the XAML controls depending on device orientation.
    // All the 'business logic' is performed in the MainPageViewModel class.
    //


    public sealed partial class MainPage : Page
    {

        // Define the app ViewModel class
        private MainPageViewModel mainPageViewModel = new MainPageViewModel();

      public MainPageViewModel MainPageViewModel
        {
            get { return this.mainPageViewModel; }
        }
        
        public MainPage()
        {
            this.InitializeComponent();

#if WINDOWS_PHONE_APP

         
            // A Windows Phone specific section

            // This extends the app's drawing container behind the status bar
            ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseCoreWindow);

            // If you prefer, you can hide the phone's status bar entirely
            // await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();

#endif


            // Create an event to fire when the device is rotated
            Window.Current.SizeChanged += Current_SizeChanged;

            // Reposition the the XAML controls to suit current orientation
            RefreshView(Window.Current.CoreWindow.Bounds.Width, Window.Current.CoreWindow.Bounds.Height);

        }




        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            // Obtain the current screen size to scale some controls appropriately
            double width = e.Size.Width;
            double height = e.Size.Height;
            RefreshView(width, height);
        }

        private void RefreshView(double width, double height)
        {

            // Re-position some XAML controls  depending on screen orientation.
            // Specifically, the smaller XAML controls displaying the forecasts
            // are moved and adjusted from horizontal to vertical and vice versa.

          
            string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();

            bool FullScreen = ApplicationView.GetForCurrentView().IsFullScreen;


            if (!FullScreen)
            {
                // Special case of snapped view on non-Phone devices

                // Hide the forecast controls and expand current conditions control
                ForecastPanel.Visibility = Visibility.Collapsed;
                
                ForecastWidth.Width = new GridLength(0);
                CurrentConditionsWidth.Width = new GridLength(width);
                CurrentConditionsHeight.Height = new GridLength(height);


            }
            else
            {
                // Show the forecast controls

                ForecastPanel.Visibility = Visibility.Visible;

                if (CurrentViewState == "Portrait")
                {
                    ForecastWidth.Width = new GridLength(0);
                    CurrentConditionsWidth.Width = new GridLength(width);

                    ForecastHeight.Height = new GridLength(height * 1 / 5);
                    CurrentConditionsHeight.Height = new GridLength(height * 4 / 5);

                    ForecastPanel.Orientation = Orientation.Horizontal;

                    ForecastPanel.SetValue(Grid.ColumnProperty, 0);
                    ForecastPanel.SetValue(Grid.RowProperty, 1);
                    ForecastPanel.SetValue(Grid.ColumnSpanProperty, 2);

                    Forecast1Grid.Height = width / 3;
                    Forecast2Grid.Height = width / 3;
                    Forecast3Grid.Height = width / 3;

                    Forecast1Grid.Width = width / 3;
                    Forecast2Grid.Width = width / 3;
                    Forecast3Grid.Width = width / 3;
                }

                if (CurrentViewState == "Landscape")
                {

                    ForecastWidth.Width = new GridLength(width * 1 / 5);
                    CurrentConditionsWidth.Width = new GridLength(width * 4 / 5);

                    ForecastHeight.Height = new GridLength(0);
                    CurrentConditionsHeight.Height = new GridLength(height);

                    ForecastPanel.Orientation = Orientation.Vertical;

                    ForecastPanel.SetValue(Grid.ColumnProperty, 1);
                    ForecastPanel.SetValue(Grid.RowProperty, 0);
                    ForecastPanel.SetValue(Grid.RowSpanProperty, 2);

                    Forecast1Grid.Height = height / 3;
                    Forecast2Grid.Height = height / 3;
                    Forecast3Grid.Height = height / 3;

                    Forecast1Grid.Width = height / 3;
                    Forecast2Grid.Width = height / 3;
                    Forecast3Grid.Width = height / 3;

                }
            }
        }
    }
}
